home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / jade / src / main.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  6KB  |  261 lines

  1. /* main.c -- Entry point for Jade
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with Jade; see the file COPYING.    If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22. #include "revision.h"
  23.  
  24. #include <string.h>
  25.  
  26. #define INIT_SCR "init"
  27.  
  28. _PR int main(int, char **);
  29. _PR int inner_main(int, char **);
  30. _PR void doconmsg(u_char *);
  31. _PR void main_init(void);
  32.  
  33. _PR bool input_lock;
  34.     bool input_lock;
  35. _PR StrMem main_strmem;
  36.     StrMem main_strmem;
  37.  
  38. _PR int recurse_depth;
  39. int recurse_depth = -1;
  40.  
  41. _PR VALUE sym_exit, sym_quit, sym_top_level, sym_command_line_args;
  42. VALUE sym_exit, sym_quit, sym_top_level, sym_command_line_args;
  43.  
  44. static u_char *init_script = INIT_SCR;
  45.  
  46. static void
  47. usage(void)
  48. {
  49.     fputs("usage: jade [SYSTEM-OPTIONS] [STANDARD-OPTIONS] [LISP-OPTIONS]\n", stderr);
  50.     sys_usage();
  51.     fputs("STANDARD-OPTIONS are,\n"
  52.     "    -rc FILE     use FILE instead of `init.jl' to boot from\n"
  53.     "    -v           print version/revision details\n"
  54.     "    -log-msgs    print all messages to standard-error as well\n"
  55.     "and LISP-OPTIONS are,\n"
  56.     "    -f FUNCTION  call the Lisp function FUNCTION\n"
  57.     "    -l FILE      load the file of Lisp forms called FILE\n"
  58.     "    -q           quit\n"
  59.     "    FILE         load FILE into a new buffer\n"
  60.     , stderr);
  61. }
  62.  
  63. static int
  64. get_main_options(int *argc_p, char ***argv_p)
  65. {
  66.     int argc = *argc_p;
  67.     char **argv = *argv_p;
  68.     VALUE head, *last;
  69.     while(argc && (**argv == '-'))
  70.     {
  71.     if((argc >= 2) && !strcmp("-rc", *argv))
  72.     {
  73.         init_script = *(++argv);
  74.         argc--;
  75.     }
  76.     else if(!strcmp("-v", *argv))
  77.     {
  78.         doconmsg(VERSSTRING "\n");
  79.         return(FALSE);
  80.     }
  81.     else if(!strcmp("-log-msgs", *argv))
  82.         log_messages = TRUE;
  83.     else if(!strcmp("-?", *argv) || !strcmp("-help", *argv))
  84.     {
  85.         usage();
  86.         return(FALSE);
  87.     }
  88.     else
  89.         break;
  90.     argc--;
  91.     argv++;
  92.     }
  93.     /* any command line args left now get made into a list of strings
  94.        in symbol "command-line-args".  */
  95.     head = sym_nil;
  96.     last = &head;
  97.     while(argc > 0)
  98.     {
  99.     *last = cmd_cons(string_dup(*argv), sym_nil);
  100.     last = &VCDR(*last);
  101.     argc--;
  102.     argv++;
  103.     }
  104.     VSYM(sym_command_line_args)->sym_Value = head;
  105.     *argc_p = argc;
  106.     *argv_p = argv;
  107.     return(TRUE);
  108. }
  109.  
  110. int
  111. main(int argc, char **argv)
  112. {
  113.     int rc;
  114.     if(!initmem())
  115.     return(10);
  116. #if 0
  117.     main_strmem.sm_FreesBeforeFlush = 3;    /* reasonable? */
  118. #endif
  119.     sm_init(&main_strmem);
  120.     values_init();
  121.     if(symbols_init())
  122.     {
  123.     rc = sys_init(argc, argv);
  124.     symbols_kill();
  125.     }
  126.     else
  127.     rc = 5;
  128.     values_kill();
  129.     sm_kill(&main_strmem);
  130.     killmem();
  131.     return(rc);
  132. }
  133.  
  134. /* This function is called from sys_init(), it completes the initialisation
  135.    process and calls the top-level event loop.  sys_init() must advance
  136.    ARGC and ARGV so they point to the next unused argument.  */
  137. int
  138. inner_main(int argc, char **argv)
  139. {
  140.     int rc = 5;
  141.  
  142.     values_init2();
  143.     lisp_init();
  144.     lispcmds_init();
  145.     lispmach_init();
  146.     buffers_init();
  147.     commandline_init();
  148.     commands_init();
  149.     edit_init();
  150.     editrect_init();
  151.     find_init();
  152.     glyphs_init();
  153.     io_init();
  154.     keys_init();
  155.     main_init();
  156.     misc_init();
  157.     movement_init();
  158.     refresh_init();
  159.     streams_init();
  160.     undo_init();
  161.     windows_init();
  162.     server_init();
  163.     sys_misc_init();
  164.     sys_windows_init();
  165. #ifdef HAVE_SUBPROCESSES
  166.     proc_init();
  167. #endif
  168.     if(get_main_options(&argc, &argv) && first_buffer())
  169.     {
  170.     VALUE arg, res;
  171.     if((arg = string_dup(init_script))
  172.        && (res = cmd_load(arg, sym_nil, sym_nil, sym_nil)))
  173.     {
  174.         rc = 0;
  175.         cursor(curr_vw, CURS_ON);
  176.         res = event_loop();
  177.     }
  178.     else if(throw_value && VCAR(throw_value) == sym_quit)
  179.     {
  180.         if(NUMBERP(VCDR(throw_value)))
  181.         rc = VNUM(VCDR(throw_value));
  182.         else
  183.         rc = 0;
  184.     }
  185.     else
  186.         doconmsg("jade: error in initialisation script\n");
  187. #ifdef HAVE_SUBPROCESSES
  188.         proc_kill();
  189. #endif
  190.     server_kill();
  191.     windows_kill();
  192.     buffers_kill();
  193.     commandline_kill();
  194.     glyphs_kill();
  195.     streams_kill();
  196.     }
  197.     return(rc);
  198. }
  199.  
  200. _PR VALUE cmd_recursive_edit(void);
  201. DEFUN_INT("recursive-edit", cmd_recursive_edit, subr_recursive_edit, (void), V_Subr0, DOC_recursive_edit, "") /*
  202. ::doc:recursive_edit::
  203. recursive-edit
  204.  
  205. Enter a new recursive-edit.
  206. ::end:: */
  207. {
  208.     VALUE res;
  209.     cursor(curr_vw, CURS_ON);
  210.     res = event_loop();
  211.     if(curr_vw)
  212.     cursor(curr_vw, CURS_OFF);
  213.     return(res);
  214. }
  215.  
  216. _PR VALUE cmd_recursion_depth(void);
  217. DEFUN("recursion-depth", cmd_recursion_depth, subr_recursion_depth, (void), V_Subr0, DOC_recursion_depth) /*
  218. ::doc:recursion_depth::
  219. recursion-depth
  220.  
  221. Returns the number of recursive-edit's deep we are, zero signifies the
  222. original level.
  223. ::end:: */
  224. {
  225.     return(make_number(recurse_depth));
  226. }
  227.  
  228. _PR VALUE cmd_input_lock(VALUE status);
  229. DEFUN("input-lock", cmd_input_lock, subr_input_lock, (VALUE args), V_SubrN, DOC_input_lock) /*
  230. ::doc:input_lock::
  231. input-lock [STATUS]
  232.  
  233. Sets or returns the status of the input lock. When this value is non-zero
  234. no user input is accepted, only messages from ARexx can get through.
  235. ::end:: */
  236. {
  237.     if(CONSP(args))
  238.     {
  239.     args = VCAR(args);
  240.     if(NILP(args))
  241.         input_lock = FALSE;
  242.     else
  243.         input_lock = TRUE;
  244.     }
  245.     if(input_lock)
  246.     return(sym_t);
  247.     return(sym_nil);
  248. }
  249.  
  250. void
  251. main_init(void)
  252. {
  253.     ADD_SUBR(subr_recursive_edit);
  254.     ADD_SUBR(subr_recursion_depth);
  255.     ADD_SUBR(subr_input_lock);
  256.     INTERN(sym_quit, "quit");
  257.     INTERN(sym_exit, "exit");
  258.     INTERN(sym_top_level, "top-level");
  259.     INTERN(sym_command_line_args, "command-line-args");
  260. }
  261.